package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.RankingBoardHistoryDetails;
import com.sc.nft.entity.vo.GetRankingBoardDetailsVO;
import com.sc.nft.enums.RankingBoardIncomeAndRewardsStatusEnum;
import com.sc.nft.mapper.RankingBoardHistoryDetailsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户排行榜历史数据详情表(奖励发放留档)(sc_ranking_board_history_details)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-08-04 10:58:17
 */
@Slf4j
@Repository
public class RankingBoardHistoryDetailsDao extends ServiceImpl<RankingBoardHistoryDetailsMapper, RankingBoardHistoryDetails> {

    public List<GetRankingBoardDetailsVO> getHistoryDetailsByIdAndUserTel(Long id, String userTel) {
        return baseMapper.getHistoryDetailsByIdAndUserTel(id, userTel);
    }

    public RankingBoardHistoryDetails getOneByRankingBoardConfIdAndRanking(Long confId, Integer ranking) {
        LambdaQueryWrapper<RankingBoardHistoryDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardHistoryDetails::getRankingBoardConfId, confId);
        wrapper.eq(RankingBoardHistoryDetails::getRanking, ranking);
        return getOne(wrapper);
    }

    public Boolean updateRankingBoardDetailsStatusByRankingBoardConfIdAndRanking(Long confId, Integer ranking, RankingBoardIncomeAndRewardsStatusEnum status) {
        LambdaUpdateWrapper<RankingBoardHistoryDetails> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardHistoryDetails::getRankingBoardConfId, confId);
        wrapper.eq(RankingBoardHistoryDetails::getRanking, ranking);
        wrapper.set(RankingBoardHistoryDetails::getStatus, status);
        wrapper.set(RankingBoardHistoryDetails::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public List<RankingBoardHistoryDetails> leaderboardsToBeAwarded() {
        LambdaQueryWrapper<RankingBoardHistoryDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardHistoryDetails::getStatus, RankingBoardIncomeAndRewardsStatusEnum.UNISSUED);
        //grantTime 时间必须在DATE函数中为当天
        wrapper.apply("DATE(grant_time) = DATE(NOW())");
        return list(wrapper);
    }

}